package com.anxin.act.listener;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anxin.act.model.service.ActModelService;
import com.anxin.act.model.service.WfActApproverService;
import com.anxin.act.utils.ActProcessUtils;
import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.StringUtils;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.sys.system.entity.SysUser;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;

/**
 * 获取审批人监听器
 * @author: liuxiangyu
 * @date: 2020/3/18 13:25
 */
@Configuration
public class ApproverListener implements ExecutionListener, TaskListener {

    @Autowired
    private WfActApproverService wfActApproverService;


    @Override
    public void notify(DelegateExecution execution) throws Exception {

    }

    @Override
    public void notify(DelegateTask delegateTask) {

    }

    /**
     * 获取审批人-会签返回集合
     * @param execution
     * @param modelId
     * @return
     */
    public List<String> getApproverList(DelegateExecution execution,String actUuid,String modelId){

        ProcessInstance processInstance = ((ExecutionEntity) execution).getProcessInstance();
        String activityId = ((ExecutionEntity) execution).getActivityId();
        List<String> approverList = wfActApproverService.getApprover(actUuid, modelId, processInstance,activityId);
        // 根据流程变量获取审批人
        this.getApproverListByVariables(execution,approverList);
        return approverList;
    }

    /**
     * 根据流程变量获取审批人信息
     * @param execution
     * @return
     */
    public void getApproverListByVariables(DelegateExecution execution,List<String> approverList){
        if (StringUtils.isNotBlank(execution.getId())){
            Map<String, Object> variables = execution.getVariables();
            Object applyChoice = variables.get("applyChoice_" + execution.getCurrentActivityId());
            if(applyChoice != null){
                JSONArray applyChoiceArray = JSONArray.parseArray(applyChoice.toString());
                for (int i = 0; i < applyChoiceArray.size(); i++) {
                    wfActApproverService.addUserIdToList(approverList, applyChoiceArray.getString(i));
                }
            }
        }
    }


    /**
     * 获取审批人
     * @param execution
     * @param actUuid
     * @param modelId
     * @return
     */
    public String getApproverString(DelegateExecution execution,String actUuid,String modelId){
        List<String> loginNameList = getApproverList(execution,actUuid,modelId);
        StringBuffer resString = new StringBuffer();
        for (String loginName : loginNameList) {
            if (StringUtils.isNotBlank(loginName)) {
                resString.append(","+loginName);
            }else {
                resString.append(loginName);
            }
        }
        return resString.toString();
    }

    /**
     * 判断流程启动时是否为这个节点选择人审批人
     * @param execution DelegateExecution
     * @param taskKey 节点标识
     * @return
     */
    public boolean selectApprover(DelegateExecution execution,String taskKey){
        Object selectApprover = execution.getVariable("applyChoice_" + taskKey);
        return selectApprover != null && JSONObject.parseArray(selectApprover.toString()).size() != 0;
    }

}
